#include <reg52.h>
#include <stdio.h>

extern unsigned char ow_reset(void);
extern void write_byte(char val);
extern unsigned char read_byte(void);
extern char Read_Temperature(void);
extern void delay(unsigned char);


extern unsigned char fnOw_Reset(void);
extern void fnWriteOneByte(unsigned char val);

#define Timer0ParmH 0xb1
#define Timer0ParmL 0xe0

//DS18B20 ROM COMMANDS
#define CMD_SEARCH_ROM 0xF0
#define CMD_READ_ROM 0x33
#define CMD_MATCH_ROM 0x55
#define CMD_SKIP_ROM 0xCC
#define CMD_ALARM_SEARCH 0xEC
//DS18B20 FUNCTION COMMANDS
#define CMD_CONVERT_T 0x44
#define CMD_WRITE_SCRATCHPAD 0x4E
#define CMD_READ_SCRATCHPAD 0xBE
#define CMD_COPY_SCRATCHPAD 0x48
#define CMD_RECALL_E2 0xB8
#define CMD_READ_POWER_SUPPLY 0xB4

#define CHAN_NUM 2
//#define MASK_BYTE_AND 0xff<<CHAN_NUM
//#define MASK_BYTE_OR MASK_BYTE_AND ^ 0xff 

//unsigned char buf[8]={0x13,0x10,0x12,0x11,0x11,0x12,0x12,0x10};

extern union
{
   	unsigned char c[2];
   	unsigned int x;
}Temperation[CHAN_NUM];

extern void fnReadLowByte(void);
extern void fnReadHighByte(void);



//=====================================================================================================
void InitSerialPort(void)
{
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      		*/
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        		*/
    TH1   = 253;                /* TH1:  reload value for 1200 baud @ 11.0592MHz   	*/
    TR1   = 1;                  /* TR1:  timer 1 run                          		*/
    TI    = 1;                  /* TI:   set TI to send first char of UART    		*/
}

void Delay1s(void)
{
	unsigned int i,j;
	for(i=0;i<200;i++)
		for(j=0;j<1000;j++){};
}

void main(void)
{
	unsigned char Temp;//,i;
/*
	union
	{
    	unsigned char c[2];
    	unsigned int x;
  	}T;
*/

	InitSerialPort();
	EA = 0x01;

	printf("Multi DS18B20 Ver 1.00\n");

//	write_byte(0x55);
//	write_byte(CMD_MATCH_ROM);
//	read_byte();

//	if(	ow_reset())
	Temp = fnOw_Reset();

	if(	Temp )
	{
		printf("Some Port Off! Presence=%02bx\n",Temp);
	}
	else
   	{
		printf("All ports on!");		
 	}

//	ow_reset();
//	write_byte(0x33);
//	write_byte(CMD_READ_ROM);

//	printf("Serial number\n");
//	for(i=0;i<8;i++)
//	{
//		Temp = read_byte();
//		printf("%02bx ",Temp);
//	}
//	printf("\n");

	while(1)
	{
//	  	ow_reset();
		fnOw_Reset();
//  		write_byte(0xCC); // Skip ROM
		fnWriteOneByte(0xcc);
// 		write_byte(0xBE); // Read Scratch Pad
		fnWriteOneByte(0xbe);
//  		T.c[1]=read_byte();
//  		T.c[0]=read_byte();
  		fnReadLowByte();
		fnReadHighByte();

 // 		ow_reset();
		fnOw_Reset();
 // 		write_byte(0xCC); //Skip ROM
		fnWriteOneByte(0xcc);
 // 		write_byte(0x44); // Start Conversion
		fnWriteOneByte(0x44);

//  		printf("Temperation=0x%04x,%02.4f\n",T.x,(float)(T.x)/16);
  		printf("Temp0=0x%04x,%02.4f",Temperation[0].x,(float)(Temperation[0].x)/16);
  		printf("  Temp1=0x%04x,%02.4f\n",Temperation[1].x,(float)(Temperation[1].x)/16);

		Delay1s();
	}
	while(1);
}
